const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");


router.post('/menu/setRoleAuth', async ctx => {
    let mod = 'roleMenu';
    let formData = ctx.request.body.formData;
    let roleId = ctx.request.body.roleId;
    await db.transaction(async (t) => {
        await db.models[mod].destroy({
            where: {
                roleId
            }
        }, )
        if(formData.length > 0) {
            await db.models[mod].bulkCreate(formData, )
        }
    })
    return ctx.suc('权限设置成功')


    // let body = ctx.request.body;
    // let records = body.records;
    // let roleId = body.roleId;
    // let checked = body.checked;
    // let ids = [];
    // records.map(row => {
    //     ids.push(row.id)
    // })
    // let menuIds = ids.join(',');
    // await db.transaction(async (t) => {
    //     await db.query(`delete from role_menu where roleId=${roleId} and menuId in(${menuIds})`, {type: QueryTypes.DELETE}, )
    //     if (checked) {
    //         let menuIdsql = `select id menuId from menu where id in (${menuIds}) union select id from menu where id in
    //                                                                                 (select pid from menu where id in (${menuIds})) `;
    //         let sqls = `MERGE into role_menu as a using (SELECT ${roleId} roleId, menuId from (${menuIdsql}) a ) b on a.roleId = b.roleId and a.menuId=b.menuId
    //             when not matched then insert (roleId, menuId, canAdd, canEdit,canDel,canAccount,canWaste) values  (b.roleId, b.menuId, 1,1,1,1,1);`
    //         await db.query(sqls, {type: QueryTypes.UPDATE}, )
    //     }
    // })
    // return ctx.suc('已更新');v
})
module.exports = router;
